package com.yhcui.monitor.task;

import com.yhcui.monitor.bean.AppInfo;
import com.yhcui.monitor.monitor.Monitor;
import com.yhcui.monitor.startup.SpringContextHolder;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.BlockingQueue;

@Slf4j
public class MonitorTask implements Runnable {

    private Monitor monitor = SpringContextHolder.getBean("connectMonitor");

    // 队列
    private BlockingQueue<AppInfo> putTaskQueue;

    // 队列
    private BlockingQueue<AppInfo> sendMsgQueue;

    public MonitorTask(BlockingQueue<AppInfo> putTaskQueue, BlockingQueue<AppInfo> sendMsgQueue) {
        this.putTaskQueue = putTaskQueue;
        this.sendMsgQueue = sendMsgQueue;
    }

    @Override
    public void run() {
        while (true) {
            try {
                AppInfo appInfo =  putTaskQueue.take();
                boolean flag = monitor.mointor(appInfo);
                if (!flag) {
                    log.info("服务出现问题，等待报警:{}",appInfo);
                    sendMsgQueue.put(appInfo);
                } else {
                    sendMsgQueue.remove(appInfo);
                }
            } catch (Exception e) {
                log.error("", e);
            }

        }
    }


}
